package com.ruoyi.product.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.coupon.api.RemoteCouponService;
import com.ruoyi.coupon.api.domain.SkuLadder;
import com.ruoyi.product.domain.*;
import com.ruoyi.product.dto.BaseAttrs;
import com.ruoyi.product.dto.ProductDTO;
import com.ruoyi.product.dto.Skus;
import com.ruoyi.product.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.service.ISpuInfoService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * spu信息Service业务层处理
 * 
 * @author 汤小洋
 * @date 2025-11-03
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SpuInfoServiceImpl implements ISpuInfoService 
{
    private final SpuInfoMapper spuInfoMapper;
    private final SpuInfoDescMapper spuInfoDescMapper;
    private final SpuImagesMapper spuImagesMapper;
    private final SpuAttrValueMapper spuAttrValueMapper;
    private final SkuInfoMapper skuInfoMapper;
    private final SkuImagesMapper skuImagesMapper;
    private final SkuAttrValueMapper skuAttrValueMapper;

    private final RemoteCouponService remoteCouponService;

    /**
     * 查询spu信息
     * 
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public SpuInfo selectSpuInfoById(Long id)
    {
        return spuInfoMapper.selectSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     * 
     * @param spuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<SpuInfo> selectSpuInfoList(SpuInfo spuInfo)
    {
        return spuInfoMapper.selectSpuInfoList(spuInfo);
    }

    /**
     * 新增spu信息
     */
    @Override
    public void insertSpuInfo(ProductDTO productDTO)
    {
        /**
         * 操作spu相关信息
         */
        // spuInfo
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(productDTO,spuInfo);
        spuInfo.setCreateTime(new Date());
        spuInfo.setUpdateTime(new Date());
        spuInfoMapper.insertSpuInfo(spuInfo);

        Long spuId = spuInfo.getId();

        // spuInfoDesc
        SpuInfoDesc spuInfoDesc = new SpuInfoDesc();
        spuInfoDesc.setSpuId(spuId);
        spuInfoDesc.setDecript(productDTO.getDecript());
        spuInfoDescMapper.insertSpuInfoDesc(spuInfoDesc);

        // spuImages
        String imagesStr = productDTO.getImages();
        if (StringUtils.isNotEmpty(imagesStr)){
           Stream.of(imagesStr.split(",")).forEach(item -> {
               SpuImages spuImages = new SpuImages();
               spuImages.setSpuId(spuId);
               spuImages.setImgUrl(item);
               spuImagesMapper.insertSpuImages(spuImages);
           });
        }

        // spuAttrValue
        List<BaseAttrs> baseAttrs = productDTO.getBaseAttrs();
        if (!CollectionUtils.isEmpty(baseAttrs)){
            baseAttrs.forEach(item -> {
                SpuAttrValue spuAttrValue = new SpuAttrValue();
                spuAttrValue.setSpuId(spuId);
                spuAttrValue.setAttrId((long) item.getAttrId());
                spuAttrValue.setAttrName(item.getAttrName());
                spuAttrValue.setAttrValue(item.getAttrValues());
                spuAttrValue.setQuickShow(item.getShowDesc());
                spuAttrValueMapper.insertSpuAttrValue(spuAttrValue);
            });
        }

        /**
         * 操作sku相关信息
         */
        // skuInfo
        List<Skus> skus = productDTO.getSkus();
        if (!CollectionUtils.isEmpty(skus)){
            skus.forEach(item -> {
                SkuInfo skuInfo = new SkuInfo();
                BeanUtils.copyProperties(item,skuInfo);
                skuInfo.setSpuId(spuId);
                skuInfo.setCategoryId(spuInfo.getCategoryId());
                skuInfo.setBrandId(spuInfo.getBrandId());
                String defaultImage = item.getImages().stream().filter(img -> img.getDefaultImg()==1).findFirst().get().getImgUrl();
                skuInfo.setSkuDefaultImg(defaultImage);
                skuInfoMapper.insertSkuInfo(skuInfo);

                Long skuId = skuInfo.getSkuId();

                // skuImages
                item.getImages().stream()
                        .filter(img -> StringUtils.isNotEmpty(img.getImgUrl()))
                        .forEach(img -> {
                            SkuImages skuImages = new SkuImages();
                            skuImages.setSkuId(skuId);
                            skuImages.setImgUrl(img.getImgUrl());
                            skuImages.setDefaultImg(img.getDefaultImg());
                            skuImagesMapper.insertSkuImages(skuImages);
                        });

                // skuAttrValue
                item.getAttr().forEach(attr -> {
                    SkuAttrValue skuAttrValue = new SkuAttrValue();
                    skuAttrValue.setSkuId(skuId);
                    skuAttrValue.setAttrId((long) attr.getAttrId());
                    skuAttrValue.setAttrName(attr.getAttrName());
                    skuAttrValue.setAttrValue(attr.getAttrValue());
                    skuAttrValueMapper.insertSkuAttrValue(skuAttrValue);
                });

                /**
                 * 操作商品的优惠、满减、会员价和积分：、skuFullReduction、memberPrice、spuBounds
                 */
                // skuLadder
                SkuLadder skuLadder = new SkuLadder();
                skuLadder.setSkuId(skuId);
                skuLadder.setFullCount((long) item.getFullCount());
                skuLadder.setDiscount(BigDecimal.valueOf(item.getDiscount()));
                remoteCouponService.addSkuLadder(skuLadder);




            });
        }






    }

    /**
     * 修改spu信息
     * 
     * @param spuInfo spu信息
     * @return 结果
     */
    @Override
    public int updateSpuInfo(SpuInfo spuInfo)
    {
        spuInfo.setUpdateTime(DateUtils.getNowDate());
        return spuInfoMapper.updateSpuInfo(spuInfo);
    }

    /**
     * 批量删除spu信息
     * 
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deleteSpuInfoByIds(Long[] ids)
    {
        return spuInfoMapper.deleteSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     * 
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deleteSpuInfoById(Long id)
    {
        return spuInfoMapper.deleteSpuInfoById(id);
    }
}
